﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClientLib.Common;

namespace ClientLib.MonitorState2
{

   public class DeviceDynamic : IComparable<DeviceDynamic>
    {
        private string device_id;
        private string monitor_id;
        private string dev_type; 

        private int scan_time=1000;

        private List<DynamicState> monitor_state;

        #region
        public int Scan_Time
        {
            get { return scan_time; }
            set { scan_time = value; }
        }

        public string Device_ID
        {
            get { return device_id; }
            set { device_id = value; }
        }

        public string Monitor_ID
        {
            get { return monitor_id; }
            set { monitor_id = value; }
        }

        public string Dev_Type
        {
            get { return dev_type; }
            set { dev_type = value; }
        }
        public List<DynamicState> Monitor_State
        {
            get { return monitor_state; }
        }

        public IEnumerable<DynamicState> Monitor_ChangeAble_State
        {
            get { return GetChangeAbleState(); }
        }
        #endregion

        #region  /////  属性函数，对设备动态属性监控
        public DeviceDynamic(string Device_id,string Monitor_id,string Dev_type, int Scan_time)
        {
            this.device_id = Device_id;
            this.monitor_id = Monitor_id;
            this.dev_type = Dev_type;
            this.scan_time = Scan_time;
            monitor_state = new List<DynamicState>();

            //AttributeValue val1 = new AttributeValue(DataType.STRING, "下变频器1");
            //DynamicState val = new DynamicState("变频器",val1);

            //monitor_state.Add(val);

            //val1 = new AttributeValue(DataType.DOUBLE, 56.2);
            //val = new DynamicState("工作频率",val1);

            //monitor_state.Add(val);

            //val1 = new AttributeValue(DataType.BOOL, true);
            //val = new DynamicState("开机状态",val1);

            //monitor_state.Add(val);
        }

        public DeviceDynamic(string Device_id, string Monitor_id,string Dev_type)
        {
            this.device_id = Device_id;
            this.monitor_id = Monitor_id;
            this.dev_type = Dev_type;
            monitor_state = new List<DynamicState>();
        }

        /*********************************************
      添加一个监控属性
      *********************************************/
        public void AddMonitorState(DynamicState state)
        {
            if (monitor_state.Contains(state))
            {
                return;
            }
            else
            {
                monitor_state.Add(state);
            }
        }

        /*********************************************
      改变监控状态值
      *********************************************/
        public void ChangeStateValue(DynamicState state)
        {
            IEnumerable<DynamicState> statelist = from varstate in monitor_state
                                                  where varstate.StateName == state.StateName
                                                  select varstate;

            
            DynamicState oldstate;
            try
            {
                oldstate = statelist.ElementAt<DynamicState>(0);

            }
            catch (Exception e)
            {
                return;
            }
            oldstate.StateValue = state.StateValue;

            return;
        }

        /*********************************************
   移除一个监控属性
    *********************************************/
        public void RemoveMonitorState(string state_name)
        {
            IEnumerable<DynamicState> statelist = from varstate in monitor_state
                                                  where varstate.StateName == state_name
                                                  select varstate;
            DynamicState move_state;
            try
            {
                move_state = statelist.ElementAt<DynamicState>(0);

            }catch(Exception)
            {
                return;
            }
            monitor_state.Remove(move_state);
            return;
        }

        /*********************************************
         获取监控状态名称集合
        *********************************************/
        public string[] GetStateName()
        {
            IEnumerable<string> name_list = this.monitor_state.Select(c=>c.StateName);

            return name_list.ToArray();
        }

        /*********************************************
         获取动态变化状态属性集合
         *********************************************/
        private IEnumerable<DynamicState> GetChangeAbleState()
        {
            IEnumerable<DynamicState> statelist = monitor_state.Where(c => c.SetAble)
                                                             .Select(c=>c);
            return statelist;
        }

        #endregion

        #region///ICompareble接口实现
        public bool Equals(DeviceDynamic other)
        {
            if ((this.device_id == other.device_id) & (this.monitor_id == other.monitor_id))
            {
                return true;
            }
            else
            {
                return false;
            }
         }

        public int CompareTo(DeviceDynamic other)
        {
            return this.device_id.CompareTo(other.device_id);
        }
        
        #endregion

    }
}
